from typing import List, Optional
from sqlmodel import SQLModel, Field
from .base import SQLModelBase
from .resBase import BaseResponse, PaginationRequest, PaginatedResponse
from datetime import date, datetime


class Carrier(SQLModel):
    """航空公司模型"""
    carrier: str = Field(description="航空公司名称")


class CarrierListResponse(BaseResponse):
    """航空公司列表响应模型"""
    data: List[Carrier] | None


class Location(SQLModel):
    """地点模型（用于出发地和目的地）"""
    dept: str = Field(description="出发地")
    
    
class DepartureListResponse(BaseResponse):
    """出发地列表响应模型"""
    data: List[Location] | None


class Destination(SQLModel):
    """目的地模型"""
    arr: str = Field(description="目的地")


class DestinationListResponse(BaseResponse):
    """目的地列表响应模型"""
    data: List[Destination] | None


class FlightQueryCondition(PaginationRequest):
    """航班查询条件模型"""
    dept: Optional[str] = Field(default=None, description="出发地")
    arr: Optional[str] = Field(default=None, description="到达地")
    carrier: Optional[str] = Field(default=None, description="航司")
    depttime_start: Optional[date] = Field(default=None, description="出发开始时间(YYYY-MM-DD)")
    depttime_end: Optional[date] = Field(default=None, description="出发结束时间(YYYY-MM-DD)")
    fno: Optional[str] = Field(default=None, description="航班号")
    min_price: Optional[float] = Field(default=None, description="最低价格")
    max_price: Optional[float] = Field(default=None, description="最高价格")



class FlightPriceQueryCondition(SQLModel):
    """航班查询条件模型"""
    dept: Optional[str] = Field(default=None, description="出发地")
    arr: Optional[str] = Field(default=None, description="到达地")
    depttime: Optional[datetime] = Field(default=None, description="出发时间")
    fno: Optional[str] = Field(default=None, description="航班号")
    

class FlightInfo(SQLModel):
    """航班信息模型"""
    id: int = Field(description="记录ID")
    dept: str = Field(description="起飞地")
    arr: str = Field(description="到达地")
    depttime: datetime = Field(description="出发时间")
    fno: str = Field(description="航班号")
    price: float = Field(description="价格")
    createtime: datetime = Field(description="记录时间")


# class SimpleFlightInfo(SQLModel):
#     """简单航班信息模型（用于 t_flight_list 表）"""
#     fno: str = Field(description="航班号")
#     dept: str = Field(description="起飞地")
#     arr: str = Field(description="到达地")
#     depttime: datetime = Field(description="出发时间")


class FlightList(SQLModel, table=True):
    """航班列表表模型"""
    __tablename__ = "v_focus_flight_group"
    id: Optional[str] = Field(default=None, max_length=16, description="唯一id", primary_key=True)
    
    fno: Optional[str] = Field(default=None, max_length=16, description="航班号")
    dept: Optional[str] = Field(default=None, max_length=3, description="起飞地")
    arr: Optional[str] = Field(default=None, max_length=3, description="到达地")
    carrier: Optional[str] = Field(default=None, max_length=3, description="航司")
    depttime: Optional[datetime] = Field(default=None, description="出发时间")


class SimpleFlightQueryResponse(BaseResponse):
    """简单航班查询响应模型"""
    data: List[FlightList] | None


class FlightQueryPaginatedResponse(PaginatedResponse):
    """航班查询分页响应模型"""
    data: List[FlightList] | None

    
class PolicysFillFocus(SQLModel, table=True):
    """航班政策填充关注表模型"""
    __tablename__ = "t_focus_flight_fill_price"
    
    id: str = Field(primary_key=True, description="主键")
    decrease_flag: int = Field(default=None, description="价格下降标志,1下降,0不下降")
    price: Optional[float] = Field(default=None, description="价格")
    createtime: Optional[datetime] = Field(default=None, description="记录时间", sa_column_kwargs={"name": "createtime"})

class FlightQueryResponse(BaseResponse):
    """航班查询响应模型"""
    data: List[PolicysFillFocus] | None
